package com.heaton.videocar.ui

import cn.com.heaton.blelibrary.ble.model.BleDevice
import com.heaton.baselib.base.BaseFragment
import com.heaton.baselib.utils.LogUtils
import com.heaton.baselib.utils.SPUtils
import com.heaton.videocar.*
import com.heaton.videocar.core.BleManager
import com.heaton.videocar.view.ArcSeekBar
import kotlinx.android.synthetic.main.fragment_cool.*

class CoolFragment: BaseFragment() {
    override fun layoutId() = R.layout.fragment_cool

    private var isPower = true

    override fun bindData() {
        BleManager.get().setBleCallback(this, object :BleManager.BleCallback(){
            override fun onChanged(device: BleDevice?, data: ByteArray) {
                super.onChanged(device, data)
                val cmd = data[1]
                if (cmd == 0x56.toByte()){//控制模式
                    val level = data[2]
                    if (level in 0..4){//制冷挡位
                        arcSeekBar.setProgress(level.toInt(), false)
                    }
                }
            }
            override fun onConnectionChanged(device: BleDevice) {
                if (!isSelfDevice(device.bleAddress))return
                when {
                    device.isConnected -> {
                        isPower = true
                        iv_power.setImageResource(R.mipmap.icon_cool_power)
                        tv_power.text = "结束"
                        iv_temp_logo.alpha = 1f
                        tv_working.show()
                        ll_level.show()
                        tv_wait.hide()
                        showLoadingDialog(false)
                    }
                    device.isDisconnected -> {
                        isPower = false
                        iv_power.setImageResource(R.mipmap.icon_cool_power_01)
                        tv_power.text = "启动"
                        iv_temp_logo.alpha = 0.3f
                        reset()
                        showLoadingDialog(false)
                    }
                }
            }
        })
    }

    private fun reset(){
        tv_working.hide()
        ll_level.hide()
        tv_wait.show()
        seekbar.progress = 0
        arcSeekBar.setProgress(0, false)
        switch_button.isChecked = false
    }

    fun resetLevel(){
        if (arcSeekBar != null){
            arcSeekBar.setProgress(0, false)
            switch_button.isChecked = false
        }
    }

    override fun bindListener() {
        iv_power.setOnClickListener {
            BleManager.get().power(isPower)
            if (isPower){
                setTimeout(1000){
                    BleManager.get().disconnect()
                }
            }else{
                val address = SPUtils.get(context, Constants.SP_CONNECTED_ADDRESS, "")
                BleManager.get().connect(address)
                showLoadingDialog(message = "启动中...")
            }
        }
        switch_button.setOnCheckedChangeListener { view, isChecked ->
            if (isChecked){
                seekbar.isEnabled = false
                seekbar.progress = 0
                arcSeekBar.isEnabled = false
//                arcSeekBar.setProgress(0, false)
            }else{
                seekbar.isEnabled = true
                arcSeekBar.isEnabled = true
            }
            BleManager.get().coolMode(!isChecked, null)
        }
        seekbar.setOnSeekBarStopChangeListener {
            BleManager.get().coolSpeed(it.progress, object :BleManager.Callback{
                override fun onCallback(data: ByteArray) {

                }
            })
        }
        arcSeekBar.setOnChangeListener(object :ArcSeekBar.OnChangeListener(){
            override fun onProgressChanged(progress: Float, max: Float, fromUser: Boolean) {
                tv_level.text = progress.toInt().toString()
                if (!fromUser)return
                BleManager.get().controlMode(progress.toInt(), object :BleManager.Callback{
                    override fun onCallback(data: ByteArray) {
                        LogUtils.loge(TAG, "")
                    }
                })
            }
        })
    }
}